﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelHideReplayButton : MonoBehaviour
{
    public List<EventAddRotation> addRotationList = new List<EventAddRotation>();
    public List<TextButton> tbList = new List<TextButton>();
    public List<CycleStateSwitch> cssList = new List<CycleStateSwitch>();
    public Button replayButton;
    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        InitTextButton();
        replayButton.onClick.AddListener(() =>
        {
            ReplayButtonClick();
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
        });
    }

    private void InitTextButton()
    {
        int length = tbList.Count;
        for (int i = 0; i < length; i++)
        {
            int tempIndex = i;
            tbList[i].Init(UConst.Str_TargetLove, () =>
            {
                TextButtonClick(tempIndex);
            });
        }
    }

    private void TextButtonClick(int index)
    {
        if (GameCtrl.Instance.curGameState == GameState.Play)
        {
            tbList[index].text.color = Color.black;
            CheckResult();
        }
    }

    private void ReplayButtonClick()
    {
        if (GameCtrl.Instance.curGameState != GameState.Play)
            return;

        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            if (tbList[i].text.color != Color.black)
            {
                if (i == 1)
                    addRotationList[i].AddRotation(-90);
                else
                    addRotationList[i].AddRotation(90);
                cssList[i].AddCount1();
            }
        }

       
    }

    private void CheckResult()
    {
        bool needCheckResult = true;
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            if (tbList[i].text.color != Color.black)
            {
                needCheckResult = false;
                break;
            }
        }

        if (needCheckResult)
        {
            bool result = true;
            for (int i = 0; i < count; i++)
            {
                if (!cssList[i].curState)
                {
                    result = false;
                    break;
                }
            }

            gameLevelItem.TriggerResult(result);
        }
    }

}
